package com.meitianhui.convenitenbuy.base;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.meitianhui.convenitenbuy.AppManager;
import com.meitianhui.convenitenbuy.util.LogUtils;
import com.meitianhui.convenitenbuy.util.SystemBarTintManager;
import com.umeng.analytics.MobclickAgent;

import io.reactivex.disposables.CompositeDisposable;
import rx.functions.Action1;

public abstract class BaseActivity extends AppCompatActivity
        implements Action1<View>, View.OnClickListener {

    protected String TAG;
    public CompositeDisposable mCompositeDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCompositeDisposable = new CompositeDisposable();
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        TAG = getClass().getSimpleName();

        initView(savedInstanceState);
        //需要的时候再去调用
        // setTranslucentStatus(R.color.transparent);
        // StatusBarUtil.StatusBarLightMode(this);
        AppManager.getAppManager().addActivity(this);
        LogUtils.d(TAG + "------>onCreate");
        //RxBus的队列
    }

    protected abstract void initView(Bundle savedInstanceState);

    @Override
    public void call(View v) {
    }

    @Override
    public void onClick(View view) {
    }

    /**
     * 改变状态栏颜色
     */
    protected void setTranslucentStatus(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(color);    // 通知栏所需颜色

        setBarColor();
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    // 设置状态栏透明
    private void setBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    /**
     * 查找View
     *
     * @param id   控件的id
     * @param <VT> View类型
     */
    protected <VT extends View> VT getViewById(@IdRes int id) {
        return (VT) this.findViewById(id);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.d(TAG + "------>onDestroy");
        AppManager.getAppManager().finishActivity(this);
        mCompositeDisposable.clear();
    }

    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
